package com.alisls.shequtao.common.model.result;

import com.alisls.shequtao.common.core.constant.ResultEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * 执行结果
 *
 * @author Ke Wang
 */
@NoArgsConstructor
@Getter
@Setter
public class DefaultResult implements Result, Serializable {

	private static final long serialVersionUID = 8377646792793477213L;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 构造函数
     * @param success
     * @param code
     * @param message
     */
    public DefaultResult(Boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    /**
     * 构造函数
     * @param resultEnum
     */
    public DefaultResult(ResultEnum resultEnum) {
        this.success = resultEnum.isSuccess();
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    public DefaultResult(Integer code, String message, boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
    }

    public static Result ofSuccess() {
        return new DefaultResult(ResultEnum.SUCCESS);
    }

    public static Result ofSuccess(String message) {
        return new DefaultResult(Boolean.TRUE, ResultEnum.DEFAULT_SUCCESS_CODE, message);
    }

    public static Result ofFail() {
        return new DefaultResult(ResultEnum.FAIL);
    }

    public static Result ofFail(String message) {
        return new DefaultResult(Boolean.FALSE, ResultEnum.DEFAULT_FAIL_CODE, message);
    }

    public static Result ofTimeout() {
        return new DefaultResult(ResultEnum.TIMEOUT);
    }

}
